// 验证用户名
(()=>{
  //验证各项
  //通过id获取用户输入的用户名
  let uname = document.getElementById("uname");
  uname.onblur = ()=>{
    let unameTip = document.getElementById("unameTip");
    if(!uname.value) {
      unameTip.style.color = "red";
      unameTip.innerHTML = "用户名不能为空";
    }else {
      if(uname.value.length<3) {
        unameTip.style.color = "red";
        unameTip.innerHTML = "用户名不能少于3位";
      }else {
        unameTip.style.color = "green";
        unameTip.innerHTML = "用户名可用";
      }
    }
  }
  uname.onfocus = ()=>{
    let unameTip = document.getElementById("unameTip");
    unameTip.style.color = "#fff";
    unameTip.innerHTML = "";
  }
})();
//验证密码
(()=>{
  //验证各项
  //通过id获取用户输入的密码
  let upwd = document.getElementById("upwd");
  upwd.onblur = ()=>{
    let upwdTip = document.getElementById("upwdTip");
    if(!upwd.value) {
      upwdTip.style.color = "red";
      upwdTip.innerHTML = "密码不能为空";
    }else {
      if(upwd.value.length<9) {
        upwdTip.style.color = "red";
        upwdTip.innerHTML = "密码不能少于9位";
      }else {
        upwdTip.style.color = "green";
        upwdTip.innerHTML = "密码可用";
      }
    }
  }
  upwd.onfocus = ()=>{
    let upwdTip = document.getElementById("upwdTip");
    upwdTip.style.color = "#fff";
    upwdTip.innerHTML = "";
  }
})();
// 验证验证码
(()=>{
  let nextCode = document.getElementById("nextCode");
  nextCode.onclick = ()=>{
    let codeImg = document.getElementById("codeImg");
    // alert(1);
    codeImg.src = `http://127.0.0.1:8848/user/v1/code?t=${new Date().getTime()}`;
  }
  let code = document.getElementById("code");
  code.onblur = ()=>{
    let codeTip = document.getElementById("codeTip");
    if(!code.value){
      codeTip.style.color = "red";
      codeTip.innerHTML = "验证码不能为空";
    }else {
      codeTip.innerHTML = "";
    }
  }
  code.onfocus = ()=>{
    let codeTip = document.getElementById("codeTip");
    codeTip.style.color = "#fff";
    codeTip.innerHTML = "";
  }
})();
//登录
(()=>{
  let btn = document.getElementById("btn");
  btn.onclick = ()=>{
    //1.通过id获取用户输入用户名、密码、邮箱、验证码
    let code = document.getElementById("code");
    let uname = document.getElementById("uname");
    let upwd = document.getElementById("upwd");
    //1.创建对象ajax
    let xhr = new XMLHttpRequest();
    //2.指定接收数据的函数
    xhr.onreadystatechange = ()=>{
      if(xhr.readyState===4 && xhr.status===200){
        // console.log(xhr.responseText);
        let codeTip = document.getElementById("codeTip");
        if(xhr.responseText == -1){
          codeTip.style.color = "red";
          codeTip.innerHTML = "验证码格式错误";
        }else if(xhr.responseText == 1){
          location.href = `http://127.0.0.1:8848/html/index.html?uname=${uname.value}`;
        }
      }
    }
    //3.指定服务器的地址
    let url = `http://127.0.0.1:8848/user/v1/login`;
    xhr.open("POST",url,true);
    //4.修改请求头信息
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    //5.发送请求
    xhr.send(`uname=${uname.value}&upwd=${upwd.value}&code=${code.value}`);
  }
})();
// 自动登录
(()=>{
  let login = ()=>{
    //1.通过id获取用户输入用户名、密码、邮箱、验证码
    let code = document.getElementById("code");
    let uname = document.getElementById("uname");
    let upwd = document.getElementById("upwd");
    //1.创建对象ajax
    let xhr = new XMLHttpRequest();
    //2.保存session数据添加属性#在客户端加密保存session数据
    xhr.withCredentials = true;
    //3.指定接收数据的函数
    xhr.onreadystatechange = ()=>{
      if(xhr.readyState===4 && xhr.status===200){
        // console.log(xhr.responseText);
        let codeTip = document.getElementById("codeTip");
        if(xhr.responseText == -1){
          codeTip.style.color = "red";
          codeTip.innerHTML = "验证码格式错误";
        }else if(xhr.responseText == 1){
          location.href = `http://127.0.0.1:8848/html/index.html?uname=${uname.value}`;
          // alert(uname.value);
        }
      }
    }
    //4.指定服务器的地址
    let url = `http://127.0.0.1:8848/user/v1/login`;
    xhr.open("POST",url,true);
    //5.修改请求头信息
    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    //6.发送请求
    xhr.send(`uname=${uname.value}&upwd=${upwd.value}&code=${code.value}`);
  }
  let autoLogin = document.getElementById("autoLogin");
  if(autoLogin.checked){
    var timer1 = setInterval(()=>{
      if(!autoLogin.checked){
        clearInterval(timer1);
        console.log(0);
        return;
      }
      login();
      console.log(autoLogin.checked);
    },10);
  }
  autoLogin.onclick = ()=>{
    if(autoLogin.checked){
      // console.log(autoLogin.checked);
      // console.log(1);
      var timer = setInterval(()=>{
        if(!autoLogin.checked){
          clearInterval(timer);
          console.log(0);
          return;
        }
        login();
        console.log(autoLogin.checked);
      },10);
    }
  }
})();